package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.epc.dao.HoHpbMapper;
import com.sumainfor.pc.epc.service.HoHpbService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@Service
public class HoHpbServiceImpl implements HoHpbService {
    private ReentrantLock lock = new ReentrantLock(false);//
    Logger log = LoggerFactory.getLogger(HoHpbServiceImpl.class);// 日志
    @Autowired
    private HoHpbMapper hoHpbMapper;
    /**
     *查询某个房源的土建验房项
     */
    @Override
    public List<Map<String, Object>> getHoHpbList(Map<String, Object> params) {
        return hoHpbMapper.getHoHpbList(params);
    }
    /**
     * 修改某个房源的土建验房项
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult updateHoHpb(Map<String, Object> params) {
        JsonResult result=new JsonResult();
        lock.lock();
        try{
            try {
                if (params.get("decorList")==null){
                    return result.putFailed("参数不正确");
                }
                //组装参数
                Map<String,Object> map=new HashMap<String,Object>();
                map.put("by", ContextUtils.getUserId());//操作人
                map.put("SysUserId",ContextUtils.getUserId());//验房人时间
                map.put("term", "4");// 更新端 Update端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                map.put("fnc",
                        this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                ParamsCommon.commonUpdateParms(map);
                map.put("InspectTime",params.get("updtm"));//验房时间
                List<Map<String,Object>> deList=(List<Map<String,Object>>)params.get("decorList");
                int num=0;
                for (Map<String,Object> deMap:deList){
                    deMap.putAll(map);
                   num=num+hoHpbMapper.updateHoHpb(deMap);
                }
                if (num==deList.size()){
                    result.putSuccess("操作成功");
                }else{
                    result.putFailed("服务器繁忙");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        finally {
            lock.unlock();
        }
        return result;
    }
}
